package cn.lg.soar.system.account.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.core.annotations.ResourceScanIgnore;
import cn.lg.soar.system.account.model.*;
import cn.lg.soar.system.account.service.ISsoServerService;
import cn.lg.soar.system.api.constant.ModuleConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ConditionalOnProperty(name = "lg.sso.server.enabled", havingValue = "true")
@ResourceScanIgnore
@Api(tags = {"单点登录服务端"})
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/sso/server")
public class SsoServerController {

    @Autowired
    private ISsoServerService ssoService;

    @ApiOperation("客户端上线")
    @PostMapping("online")
    public HttpResult<? extends SsoServerInfo> online(@RequestBody SsoClientInfo clientInfo) {
        return HttpResult.data(ssoService.online(clientInfo));
    }

    @ApiOperation("登录验证")
    @GetMapping("validation")
    public HttpResult<? extends SsoUser> validation(SsoValidation validation) {
        AssertUtil.notEmpty(validation.getSsoToken(), "缺少token");
        AssertUtil.notEmpty(validation.getTicket(), "缺少ticket");
        return HttpResult.data(this.ssoService.validation(validation));
    }

    @ApiOperation("退出登录通知")
    @GetMapping("logout-notify")
    public HttpResult<Void> logout(SsoNotifyServerLogout user) {
        AssertUtil.notEmpty(user.getSsoToken(), "缺少token");
        ssoService.logout(user);
        return HttpResult.success();
    }

    @ApiOperation("客户端下线")
    @GetMapping("offline")
    public HttpResult<Void> offline(@RequestParam String token) {
        AssertUtil.notEmpty(token, "缺少token");
        ssoService.offline(token);
        return HttpResult.success();
    }

}
